package com.ph.util;

/**
 * @author sschina-tech2
 *
 */
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

/**
 * 日期转换相关函数
 */
public class DateUtil {
	public static final String YMD = "yyyy-MM-dd";
	public static final String HMS = "HH:mm:ss";
	public static final String STARTCOLOCK = " 00:00:00";
	public static final String ENDCOLOCK = " 23:59:59";

	public static Date string2Date(String dateText, String format) {

		if (dateText == null || dateText.equals("")) {
			return null;
		}
		DateFormat df;
		try {
			if (format == null || format.equals("")) {
				df = new SimpleDateFormat(YMD);
			} else {
				df = new SimpleDateFormat(format);
			}
			df.setLenient(false);
			return df.parse(dateText);
		} catch (ParseException e) {
			System.out.println("错误：" + e);
			return null;
		}
	}

	public static String getCurrentDateString() {
		return date2String(getCurrentDateTime(), YMD);
	}

	public static String getCurrentDateTimeString() {
		String pattern = YMD + "-" + HMS;
		return date2String(getCurrentDateTime(), pattern);
	}

	public static String date2String(Date date, String pattern) {

		if (date == null) {
			return null;
		}

		try {
			SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
			sfDate.setLenient(false);
			return sfDate.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Timestamp string2Time(String dateString, String pattern) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);// 设定格式
			dateFormat.setLenient(false);
			return new Timestamp(dateFormat.parse(dateString).getTime());
			// return sfDate.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String time2String(Timestamp dateTimeStamp, String pattern) {
		if (dateTimeStamp == null) {
			return null;
		}

		try {
			SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
			sfDate.setLenient(false);
			return sfDate.format(dateTimeStamp);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String fmtDate(Date date) {
		if (date == null) {
			return "";
		}

		try {
			SimpleDateFormat sfDate = new SimpleDateFormat(YMD);
			sfDate.setLenient(false);
			return sfDate.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	public static Date stringToDate(String dateText) {
		return string2Date(dateText, null);
	}

	/**
	 * 取当前日期时间
	 *
	 * @return
	 */
	public static Date getCurrentDateTime() {
		java.util.Calendar calNow = java.util.Calendar.getInstance();
		java.util.Date dtNow = calNow.getTime();
		return dtNow;
	}

	/**
	 * 转化类型
	 * @param date
	 * @return
	 */
	public static Timestamp date2Timestamp(Date date) {
		return new Timestamp(date.getTime());
	}

	/**
	 * 本年
	 *
	 * @return
	 */
	public static String getThisYear() {

		return date2String(getCurrentDateTime(), "yyyy");
	}

	/**
	 * 本月
	 *
	 * @return
	 */
	public static String getThisMonth() {

		return date2String(getCurrentDateTime(), "M");
	}

	/**
	 * 本月，汉字
	 *
	 * @return
	 */
	public static String getThisMonthChar() {
		String str = date2String(getCurrentDateTime(), "M");
		String charMonth = "";
		switch(Integer.parseInt(str))
		{
		case 1:charMonth = "一月";
		case 2:charMonth = "二月";
		case 3:charMonth = "三月";
		case 4:charMonth = "四月";
		case 5:charMonth = "五月";
		case 6:charMonth = "六月";
		case 7:charMonth = "七月";
		case 8:charMonth = "八月";
		case 9:charMonth = "九月";
		case 10:charMonth = "十月";
		case 11:charMonth = "十一月";
		case 12:charMonth = "十二月";
		default:charMonth = "";
		}

		return charMonth;
	}

	public static void main(String[] args) {
		// Calendar calendar = Calendar.getInstance();

		// System.out.println("sunday: " + Calendar.SUNDAY);
		// System.out.println("monday: " + Calendar.MONDAY);
		// System.out.println("tuesday: " + Calendar.TUESDAY);
		// System.out.println("wendesday: " + Calendar.WEDNESDAY);
		// System.out.println("thursday: " + Calendar.THURSDAY);
		// System.out.println("friday: " + Calendar.FRIDAY);
		// System.out.println("saturday: " + Calendar.SATURDAY);
		System.out.println("saturday: " + getThisMonth());
	}
}
